<div class="content">
    <div class="form-row row-fluid" id="div_form_devis">
        {% include "GdsVenteBundle::Devis/form_devis.html.twig" %}

    </div>

</div>
<script type="text/javascript">
    Array.prototype.remove = function(v) {
        this.splice(this.indexOf(v) == -1 ? this.length : this.indexOf(v), 1);
    }
    $(document).ready(function() {
        $('#departement').val({{user.departement.id}})
        articles = new Array();
        $('#start').show()
        $('#now').val('{{time}}')
        $('#heure').val('{{heure}}')
        $('#start').click(function() {
            $(this).hide()
            $('#prev').show()
            timerId = setInterval(time, 1000);
        })
        $('#prev').click(function() {
            $(this).hide()
            $('#start').show()
            clearInterval(timerId);
            $('#now').val('{{time}}')
            $('#heure').val('{{heure}}')
        })
        $('#retour2').click(function() {
            clearInterval(timerId);
        })
        $('#submit_form_devis').click(function() {
            $(this).hide()
            $('#submit_form_devis_print').hide()
            if ($('#submit_form_devis').val() != 'Duppliquer') {
                $('#gds_ventebundle_devistype_reference').focus()
                if ($('#form_devis').valid()) {
                    $.ajax({
                        url: '{{path("gds_devis_edit")}}',
                        type: "POST",
                        datatype: 'json',
                        data: $('#form_devis').serialize() + '&articles={"articles":' + JSON.stringify(articles) + '}&heure=' + $('#heure').val() + '&id=' + '{{id}}',
                        success: function(data) {
                            if (data.success) {
                                $.msgBox({
                                    title: "Devis",
                                    content: "Enregistrement Effectué avec succés",
                                    type: "info",
                                    buttons: [{value: "OK"}],
                                    success: function(result) {
                                        location.reload();
                                    }
                                });
                            } else {
                                $.msgBox({
                                    title: "Devis",
                                    content: "Erreur D'enregistrement",
                                    type: "error",
                                    buttons: [{value: "OK"}],
                                    success: function(result) {
                                        $('#submit_form_devis').show("slow")
                                        $('#submit_form_devis_print').show("slow")
                                    }
                                });
                            }

                        },
                        error: function(e) {
                            $.msgBox({
                                title: "Devis",
                                content: "Erreur D'enregistrement",
                                type: "error",
                                buttons: [{value: "OK"}],
                                success: function(result) {
                                    $('#submit_form_devis').show("slow")
                                    $('#submit_form_devis_print').show("slow")
                                }
                            });
                        }
                    })
                }
            }
        })
        $('#submit_form_devis_print').click(function() {
            $(this).hide()
            $('#submit_form_devis').hide()
            if ($('#submit_form_devis').val() != 'Duppliquer') {
                $('#gds_ventebundle_devistype_reference').focus()
                if ($('#form_devis').valid()) {
                    $.ajax({
                        url: '{{path("gds_devis_edit")}}',
                        type: "POST",
                        datatype: 'json',
                        data: $('#form_devis').serialize() + '&articles={"articles":' + JSON.stringify(articles) + '}&heure=' + $('#heure').val() + '&id=' + '{{id}}',
                        success: function(data) {
                            if (data.success) {
                                Imprimer(data.id)
                            } else {
                                $.msgBox({
                                    title: "Devis",
                                    content: "Erreur D'enregistrement",
                                    type: "error",
                                    buttons: [{value: "OK"}],
                                    success: function(result) {
                                        $('#submit_form_devis').show("slow")
                                        $('#submit_form_devis_print').show("slow")
                                    }
                                });
                            }

                        },
                        error: function(e) {
                            $.msgBox({
                                title: "Devis",
                                content: "Erreur D'enregistrement",
                                type: "error",
                                buttons: [{value: "OK"}],
                                success: function(result) {
                                    $('#submit_form_devis').show("slow")
                                    $('#submit_form_devis_print').show("slow")
                                }
                            });
                        }
                    })
                }
            }
        })
        $('#gds_ventebundle_devistype_date').datepicker();
        $('#form_devis').validate();
        $container = $('#gds_ventebundle_lignedevistype');
        index = 0;
        nb_article = 1;
        {% for l,ligne in lignes %}
        add_ligne()
        $('#td_reference_' +{{l}}).html('{{ligne.article.reference}}')
        $('#td_designation_' +{{l}}).html('{{ligne.article.designation}}')
        $('#gds_ventebundle_devistype_lignesDevis_' +{{l}} + '_quantite').val('{{ligne.quantite}}')
        $('#gds_ventebundle_devistype_lignesDevis_' +{{l}} + '_prix_unitaire_ht').val('{{ligne.prixUnitaireHt}}')
        tva_twig = {{ligne.tauxTva}}
        tva_twig = tva_twig.toFixed(2)
        $('#td_tva_' +{{l}}).html(tva_twig + '%')
        $('#gds_ventebundle_devistype_lignesDevis_' +{{l}} + '_taux_remise').val('{{ligne.tauxRemise}}')
        $('#gds_ventebundle_devistype_lignesDevis_' +{{l}} + '_article').val('{{ligne.article.id}}')
        var x = new Object();
        x.reference = $('#td_reference_' + {{l}}).html()
        x.designation = $('#td_designation_' + {{l}}).html()
        x.taux_tva = '{{ligne.tauxTva}}'
        x.remise = '{{ligne.tauxRemise}}'
        x.remise_max = '{{ligne.article.remiseMax}}'
        x.prix_vente = '{{ligne.article.prixVente}}'
        x.quantite = '{{ligne.quantite}}'
        x.prix_vente_min_ttc = x.prix_vente * (1 + (x.taux_tva / 100)) * (1 - (x.remise_max / 100))
        x.name = x.reference + ' : ' + x.designation
        x.id = '{{ligne.article.id}}'
        articles[{{l}}] = x
        completerChamp('{{l}}')
        {% endfor %}
        RefraichirTaxe()
        setTousQuantite()
    });
    function completerChamp(i) {
        var article = getArticle($('#gds_ventebundle_devistype_lignesDevis_' + i + '_article').val())
        var quantite = article.quantite;
        var prix_vente = article.prix_vente;
        var tva = article.taux_tva;
        var remise = article.remise;
        var puttc = prix_vente
        puttc *= (1 + tva / 100)
        puttc *= (1 - remise / 100)
        puttc = puttc.toFixed(3)
        $('#prix_u_ttc_' + i).val(puttc);
        var total = puttc * quantite
        $('#td_total_' + i).html(total.toFixed(3));
        $('#gds_ventebundle_devistype_lignesDevis_' + i + '_taux_remise').spinner({
            min: 0,
            max: article.remise_max,
            step: 1,
            places: 2,
            showOn: 'both'
        });
    }

    function retourListeDevis2() {
        $.ajax({
            url: "{{ path('gds_liste_devis') }}",
            data: 'retour=true' + '&numero=' + lf[0] + '&raison_social=' + lf[1] + '&date1=' + lf[2] + '&date2=' + lf[3] + '&heure1=' + lf[4] + '&heure2=' + lf[5]
                    + '&reference=' + lf[6] + '&designation=' + lf[7] + '&ttc1=' + lf[8] + '&ttc2=' + lf[9] + '&is_cloture=' + lf[10],
            success: function(data) {
                $('#div_liste_devis').replaceWith(data)
                $('#filtre_numero').val(lf[0])
                $('#filtre_raison_social').val(lf[1])
                $('#filtre_date1').val(lf[2])
                $('#filtre_date2').val(lf[3])
                $('#filtre_reference').val(lf[6])
                $('#filtre_designation').val(lf[7])
                $('#filtre_ttc1').val(lf[8])
                $('#filtre_ttc2').val(lf[9])
            }
        })
    }
</script>

<input type="hidden" id="heure"/>